import ResManage from "../Framework/Managers/ResManage";
import { ResConfig } from "../Framework/SysConfig/SysConfig";




const { ccclass, property } = cc._decorator;

@ccclass
export default class GameApplication extends cc.Component {

    public static Instance: GameApplication = null;

    onLoad() {
        if (GameApplication.Instance == null) {
            GameApplication.Instance = this;
        }
        else {
            this.destroy();
            return;
        }
    }

    // 初始化物理设置
    initPhy(_usePhy: boolean, _isDebug: Boolean, _gravity: cc.Vec2) {
        let phyManager: cc.PhysicsManager = cc.director.getPhysicsManager();
        if (_usePhy) {
            phyManager.enabled = _usePhy;
            if (_isDebug) {
                phyManager.debugDrawFlags = cc.PhysicsManager.DrawBits.e_shapeBit || cc.PhysicsManager.DrawBits.e_jointBit;
            } else {
                phyManager.debugDrawFlags = 0;
            }
            //console.log("系统默认重力加速度：",this.phyManager.gravity);
            phyManager.gravity = _gravity;
        }
        else {
            phyManager.enabled = false;
        }
    }


    // 游戏入口
    public EnterGame() {
        ResManage.Instance.loadAllBoundles(ResConfig.AllBundle, (now: number, total: number) => {
            console.log(now, total);
        }, () => {
            console.log('配置的AB包全部加载完成!!!');
            this._preLoadAssetByBoundle();
        });
    }

    // 预加载Bundle中的资源到内存中
    private async _preLoadAssetByBoundle() {
        await ResManage.Instance.advLoadAssetFromBundle(ResConfig.SoundBundle, cc.AudioClip, (now: number, total: number) => {
            console.log(now, total);
        });
        console.log('SoundBundle AB包资源加载完毕');

        await ResManage.Instance.advLoadAssetFromBundle(ResConfig.TablesBundle, cc.TextAsset, (now: number, total: number) => {
            console.log(now, total);
        });
        console.log('TablesBundle AB包资源加载完毕');

        //进入场景
        this._EnterLoadingScene();
    }

    
    private _EnterLoadingScene() {
        //------声音测试
        //SoundManage.Instance.playBgMusic('soundMusic/background', false);
        //SoundManage.Instance.playSound('soundEffect/attack1');

        //------事件测试
        // EventManage.Instance.on(EventConfig.xxx, this.callFun, this);
        // EventManage.Instance.dispatchEvent(EventConfig.xxx, 123);

        //------表格数据测试, 一般在点击开始游戏时候
        // TablesManage.Instance.ParseTableData(true, (now: number, total: number) => {
        //     console.log(now, total);
        // });
        // this.scheduleOnce(() => {
        //     let gouMai: GouMaiData[] = TablesManage.Instance.searchTables(TablesConfig.TableType.Table_Type_GouMai);
        //     let jiaGe: JiaGeData[] = TablesManage.Instance.searchTables(TablesConfig.TableType.Table_Type_JiaGe);

        //     console.log('===>', gouMai);
        //     console.log('+++>', jiaGe);
        // }, 2)

        //------全局定时器测试
        // let one = TimerManage.Instance.ScheduleOnce((ss: any) => {
        //     console.log('====>>>111',ss)
        // }, 3, 0)

        // let two = TimerManage.Instance.Schedule((ss: any) => {
        //     console.log('====>>>222')
        // },2, 2, 5)

        // TimerManage.Instance.Unschedule(one)
        // TimerManage.Instance.UnscheduleAllCallbacks();
        

        //UI管理进行测试
        // let parent: cc.Node = UIManage.Instance.PopUp;
        // UIManage.Instance.ShowUIPannelByID(UIConfig.UI_TYPE_ID.UI_UID_TEST, parent, (asset: cc.Node) => {
        //     let spritName: string = UIPlanInfo.getInstance().getPlanInfo(UIConfig.UI_TYPE_ID.UI_UID_TEST)._uiName;
        //     let com_Script = asset.getComponent(spritName);
        //     com_Script.init(1);

        // });

        // this.scheduleOnce(() => {
        //     console.log('+++++++++++', cc.assetManager.assets)
        // },3)
        
    }



}
